package com.pipi.controller;

import com.pipi.entity.Result;
import com.pipi.entity.User;
import com.pipi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    //用户：

    @RequestMapping("/autoRegistration")
    @ResponseBody
    public Result autoRegistration(@RequestBody User user) {
        userService.addUser(user);
        return Result.ok();
    }

    @RequestMapping("/getUserInfo")
	public Result getUserInfo() {
        System.out.println("getUserInfo");
        List<User> users = userService.getUserInfo();
        return Result.ok(users);
    }
    @RequestMapping("/getUserInfoByOpenId")
    public Result getUserInfoByOpenId(String userOpenid) {
        System.out.println("getUserInfoByOpenId");
        User user = userService.getUserInfoByOpenId(userOpenid);
        return Result.ok(user);
    }


    @RequestMapping("/updateUserInfo")
    @ResponseBody
	public Result updateUserInfo(@RequestBody User userInfo){
        System.out.println("updateUserInfo");
        System.out.println(userInfo);
        userService.updateUserInfo(userInfo);
        return Result.ok();
    }

    //护理人员：
    @RequestMapping(value = "/loginNursingStaff", method = RequestMethod.POST)
    public Result loginNursingStaff(@RequestBody Map<String, String> payload) {
        String username = payload.get("username");
        String password = payload.get("password");

        System.out.println("loginNursingStaff");
        System.out.println("username:" + username);
        System.out.println("password:" + password);

        Integer loginNursingStaff = userService.loginNursingStaff(username, password);
        if (loginNursingStaff == 1) {
            return Result.ok("登录成功！");
        } else {
            return Result.error("账号或密码错误！");
        }
    }

    /**
     * 通过openid获取护理人员信息
     * @param
     * @return
     */
    @RequestMapping("/getNursingStaffInfoByOpenId")
    public Result getNursingStaffInfoByOpenId(String openId){
        Map<String, String> user = userService.getNursingStaffInfoByOpenId(openId);
        return Result.ok(user);
    }

    //查询所有人员
    @RequestMapping("/getAllUser")
    public Result getAllUser(String userStatus){
        List<User> allUser = userService.getAllUser(userStatus);
        return Result.ok(allUser);
    }

}
